/**
 * @filename:StkOtherInController Feb 5, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd.
 * All right reserved.
 */
package com.wonder.app.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.PrdFeed;
import com.wonder.app.domain.StkTransfer;
import com.wonder.app.request.PrdFeedRequest;
import com.wonder.app.request.StkTransferRequest;
import com.wonder.app.service.PrdFeedService;
import com.wonder.app.service.StkTransferService;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.framework.authorization.CheckAdminToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.function.ServerRequestExtensionsKt;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "直接调拨单")
@RestController
@RequestMapping("/app/StkTransfer")
public class StkTransferController {

@Autowired
private StkTransferService stkTransferService;


    @Autowired
    private CheckAdminToken checkAdminToken;

    @Autowired
    private SysAdminService adminService;


    @ApiOperation(value = "直接调拨单列表")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String,Object> index(@RequestBody StkTransferRequest stkTransferRequest) {
        Page<StkTransfer> page = new Page<StkTransfer>(stkTransferRequest.getPage(), stkTransferRequest.getLimit());
        IPage<StkTransfer> list = stkTransferService.selectList(stkTransferRequest, page);
        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data", CommonPage.restPage(list));
        return AjaxMap.success(retMap);
    }
    /**
     * @explain 查看对象明细
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询stktransfer明细")
    @ResponseBody
    @PostMapping(value = "/stktransferdetail")

    public AjaxResult<Map<String,Object>> getStkTransferDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = stkTransferService.getStkTransferDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

    @ApiOperation(value = "调拨单列表(手机端)")
    @RequestMapping(value = "/get_transfer_list", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getTransferList(@RequestBody Map<String,Object> map) throws Exception{

        Map<String,Object> data = stkTransferService.getTransferList(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }


    @ApiOperation(value = "生成调拨单")
    @RequestMapping(value = "/create_transfer", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> createTransfer(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        boolean res = stkTransferService.createTransfer(userId,userName,map);
        if (res){
            return AjaxResult.success();
        } else {
            return AjaxResult.failed();
        }

    }

    @ApiOperation(value = "生成调拨单临时数据")
    @RequestMapping(value = "/create_transfer_temp", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> createTransferTemp(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        boolean res = stkTransferService.createTransferTemp(userId,userName,map);
        if (res){
            return AjaxResult.success();
        } else {
            return AjaxResult.failed();
        }

    }


    @ApiOperation(value = "生成调拨单临时数据(调拨单扫拖)")
    @RequestMapping(value = "/create_transfer_temp_by_tray", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> createTransferTempByTray(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        boolean res = stkTransferService.createTransferTempByTray(userId,userName,map);
        if (res){
            return AjaxResult.success();
        } else {
            return AjaxResult.failed();
        }

    }


    @ApiOperation(value = "获取调拨单的临时数据")
    @RequestMapping(value = "/get_transfer_temp", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getTransferTemp(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        Map<String,Object>  data = stkTransferService.getTransferTemp(userId,userName,map);
        if (!VtuzxUtil.isEmpty(data)){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }



    @ApiOperation(value = "删除调拨单临时数据")
    @RequestMapping(value = "/delete_transfer_temp", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> deleteTransferTemp(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        boolean res = stkTransferService.deleteTransferTemp(userId,userName,map);
        if (res){
            return AjaxResult.success();
        } else {
            return AjaxResult.failed();
        }

    }



}
